package com.myspring.lab.control;

import com.myspring.lab.dao.DbProject;
import com.myspring.lab.entity.Project;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;

@Controller
public class doProject {
    @RequestMapping(value = "/getproject",method = RequestMethod.GET)
    public String getProject(Model model){
        DbProject dbProject = new DbProject();
        Project[] projects = dbProject.getAllProject();
        model.addAttribute("Projects",projects);
        return "redirect:main";
    }
    @RequestMapping(value = "/addproject",method = RequestMethod.POST)
    public String addproject(HttpServletRequest request, Model model){
        DbProject dbProject = new DbProject();
        Project project = new Project();
        project.setProNo(request.getParameter("proid"));
        project.setProUser(request.getParameter("prouser"));
        project.setProName(request.getParameter("proname"));
        project.setProDetail(request.getParameter("prodetail"));
        project.setProGrade(request.getParameter("prograde"));
        project.setProFee(Integer.parseInt(request.getParameter("profee")));
        if(dbProject.addProject(project)){
            model.addAttribute("status","新建项目成功");
        }else{
            model.addAttribute("status","新建项目失败");
        }
        return "forward:add";
    }
    @RequestMapping(value = "/editproject",method = RequestMethod.POST)
    public String editproject(HttpServletRequest request){
        DbProject dbProject = new DbProject();
        Project project = new Project();
        project.setProNo(request.getParameter("proid"));
        project.setProUser(request.getParameter("prouser"));
        project.setProName(request.getParameter("proname"));
        project.setProGrade(request.getParameter("prograde"));
        project.setProFee(Integer.parseInt(request.getParameter("profee")));
        project.setProDetail(request.getParameter("prodetail"));
        dbProject.modifyProject(project);
        return "redirect:edit";
    }
    @RequestMapping(value = "/deleteproject",method = RequestMethod.POST)
    public void delete(HttpServletRequest request){
        DbProject dbProject = new DbProject();
        Project project = new Project();
        project.setProNo(request.getParameter("proid"));
        project.setProUser(request.getParameter("prouser"));
        project.setProName(request.getParameter("proname"));
        project.setProGrade(request.getParameter("prograde"));
        project.setProFee(Integer.parseInt(request.getParameter("profee")));
        project.setProDetail(request.getParameter("prodetail"));
        dbProject.deleteProject(project);
    }
    @RequestMapping(value = "/searchproject", method=RequestMethod.POST)
    public String search(@RequestParam("proname") String name, Model model){
        DbProject dbProject = new DbProject();
        Project [] projects = dbProject.queryProject(name);
        model.addAttribute("projects",projects);
        return "search";
    }
}
